<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>试题库管理</span>
        <div class="extra">
          <el-button type="primary" @click="add">新增试题</el-button>
        </div>
      </div>
    </template>
    <el-form :inline="true" :model="testQuestion" class="demo-form-inline">
      <el-form-item label="试题编号：">
        <el-input v-model="testQuestion.id" placeholder="请输入试题编号"></el-input>
      </el-form-item>
      <el-form-item label="试题类型：">
        <el-select v-model="testQuestion.type" placeholder="请选择试题类型" style="width: 100%;">
          <el-option label="单选" value="单选"></el-option>
          <el-option label="多选" value="多选"></el-option>
          <el-option label="判断" value="判断"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题目描述：">
        <el-input v-model="testQuestion.question" placeholder="请输入..."></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="show">查询</el-button>
        <el-button @click="clear">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="deleteByIds">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table max-height="400" highlight-current-row ref="multipleTable" tooltip-effect="dark"
      @selection-change="handleSelectionChange" :data="tableData" style="margin: 0 auto; width: 100%;">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="编号" header-align="center" align="center" width="80px"></el-table-column>
      <el-table-column prop="question" label="试题描述" header-align="center" align="center"></el-table-column>
      <el-table-column prop="type" label="类型" header-align="center" align="center" width="48px"></el-table-column>
      <el-table-column prop="ans" label="答案" header-align="center" align="center" width="48px"></el-table-column>
      <el-table-column prop="a" label="选项A" header-align="center" align="center"></el-table-column>
      <el-table-column prop="b" label="选项B" header-align="center" align="center"></el-table-column>
      <el-table-column prop="c" label="选项C" header-align="center" align="center"></el-table-column>
      <el-table-column prop="d" label="选项D" header-align="center" align="center"></el-table-column>
      <el-table-column label="操作" width="250" header-align="center" align="center">
        <template slot-scope="scope">
          <el-button v-show="scope.row.agencyId === 0 && myInfo.role === '机构管理员'" type="primary" plain
            @click="">无权操作共享试题
          </el-button>
          <el-button v-show="scope.row.agencyId !== 0 || (scope.row.agencyId === 0 && myInfo.role === '超级管理员')"
            type="primary" class="el-icon-edit" circle plain @click="updateInfo(scope.row.id)">
          </el-button>
          <el-button v-show="scope.row.agencyId !== 0 || (scope.row.agencyId === 0 && myInfo.role === '超级管理员')"
            type="danger" class="el-icon-delete" circle plain @click="deleteById(scope.row.id)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination style="margin-top: 20px;text-align: right;" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :background="background" :current-page="currentPage" :page-sizes="[5, 10]"
      :page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
    </el-pagination>

    <el-dialog title="试题信息" :visible.sync="dialogVisible" :before-close="close" width="30%">
      <el-form :model="testQuestion" :label-width="'100px'">
        <el-form-item label="题目：" :rules="[{ required: true }]">
          <el-input v-model="testQuestion.question" placeholder="请输入题目" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="题目类型：" :rules="[{ required: true }]">
          <el-select v-model="testQuestion.type" placeholder="请选择题目类型" style="width: 100%;">
            <el-option label="单选" value="单选"></el-option>
            <el-option label="多选" value="多选"></el-option>
            <el-option label="判断" value="判断"></el-option>
          </el-select>
        </el-form-item>
        <div v-show="testQuestion.type != '判断'">
          <el-form-item label="选项A：" :rules="[{ required: true }]">
            <el-input v-model="testQuestion.a" placeholder="请输入..." autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="选项B：" :rules="[{ required: true }]">
            <el-input v-model="testQuestion.b" placeholder="请输入..." autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="选项C：" :rules="[{ required: true }]">
            <el-input v-model="testQuestion.c" placeholder="请输入..." autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="选项D：" :rules="[{ required: true }]">
            <el-input v-model="testQuestion.d" placeholder="请输入..." autocomplete="off"></el-input>
          </el-form-item>
        </div>
        <!--单选          -->
        <el-form-item label="答案" v-show="testQuestion.type == '单选'" :rules="[{ required: true }]">
          <el-select v-model="oneAns" placeholder="请选择答案" style="width: 100%;">
            <el-option label="A" value="A"></el-option>
            <el-option label="B" value="B"></el-option>
            <el-option label="C" value="C"></el-option>
            <el-option label="D" value="D"></el-option>
          </el-select>
        </el-form-item>
        <!--多选          -->
        <el-form-item label="答案" multiple v-show="testQuestion.type == '多选'" :rules="[{ required: true }]">
          <el-select v-model="moreAns" multiple placeholder="请选择答案" style="width: 100%;">
            <el-option label="A" value="A"></el-option>
            <el-option label="B" value="B"></el-option>
            <el-option label="C" value="C"></el-option>
            <el-option label="D" value="D"></el-option>
          </el-select>
        </el-form-item>
        <!--判断          -->
        <el-form-item label="答案" multiple v-show="testQuestion.type == '判断'" :rules="[{ required: true }]">
          <el-select v-model="testQuestion.ans" placeholder="请选择答案" style="width: 100%;">
            <el-option label="对" value="对"></el-option>
            <el-option label="错" value="错"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer"></div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false; clear()">取消</el-button>
        <el-button type="primary" @click="addForm()">确定</el-button>
      </div>
    </el-dialog>

  </el-card>
</template>
<script>


import {
  addTestQuestion,
  changeTestQuestion,
  deleteTestQuestion,
  getAllTestQuestion
} from "@/api/testQuestion";
import { getInfo } from "@/api/user";

export default {
  data() {
    return {
      background: true,
      // 每页显示的条数
      pageSize: 5,
      // 总记录数
      totalCount: 100,
      // 当前页码
      currentPage: 1,
      // 添加数据对话框是否展示的标记
      dialogVisible: false,
      // 表格数据
      tableData: [],
      addOrUpdate: true,//true为add
      testQuestion: {},
      form: {},
      posts: [],
      multipleSelection: [],
      selectedIds: [],
      testQuestionList: [],
      formLabelWidth: "140px",
      //选择答案
      oneAns: '',
      moreAns: [],

      myInfo: {},
    }
  },
  methods: {
    show() {
      getAllTestQuestion(this.testQuestion, this.currentPage, this.pageSize).then((res) => {
        if (res.data.code === 1) {
          this.totalCount = res.data.data.total;
          this.tableData = res.data.data.rows;
          //console.log(this.tableData)
          if (this.tableData.length === 0 && this.currentPage > 1) {
            this.currentPage -= 1;
            this.show();
          }
        }
      }).catch((e) => {
      })
    },

    clear() {
      this.testQuestion = {};
      this.show();
    },
    add() {
      this.testQuestion = {};
      this.addOrUpdate = true;
      this.dialogVisible = true;
    },
    addForm() {
      if (this.testQuestion.type === "单选") {
        this.testQuestion.ans = this.oneAns;
      } else if (this.testQuestion.type === "多选") {
        let ans = '';
        for (let i = 0; i < this.moreAns.length; i++) {
          ans = ans + this.moreAns[i];
        }
        this.testQuestion.ans = ans;

      }
      if (this.addOrUpdate) {
        addTestQuestion(this.testQuestion).then(res => {
          if (res.data.code === 1) {
            this.$message.success("保存成功")
            this.dialogVisible = false;
            this.moreAns = [];
            this.oneAns = '';
            this.show();
          } else {
            this.$message.error(res.data.msg)
          }
        }).catch(e => {
        });
      } else {
        //更新
        changeTestQuestion(this.testQuestion).then(res => {
          console.log(res)
          if (res.data.code === 1) {
            this.$message.success("更新成功")
            this.dialogVisible = false;
            this.show()
          } else {
            this.$message.error(res.data.msg)
          }
        }).catch(e => {
        });
      }

    },
    close() {
      this.dialogVisible = false;
      this.testQuestion = {}
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    updateInfo(id) {
      this.addOrUpdate = false;
      this.testQuestion = this.tableData.filter(data => data.id == id)[0];
      if (this.testQuestion.type === "单选") {
        this.oneAns = this.testQuestion.ans;
      } else if (this.testQuestion.type === "多选") {
        this.moreAns = this.testQuestion.ans.split('');
      }
      this.dialogVisible = true;
    },
    deleteById(id) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(this.tableData.filter(data => data.id === id)[0]);
      this.deleteByIds()
    },
    deleteByIds() {
      // 弹出确认提示框
      this.$confirm("你确认删除该条信息吗？", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //用户点击确认按钮
        //1. 创建id数组, 从 this.multipleSelection 获取即可
        for (let i = 0; i < this.multipleSelection.length; i++) {
          this.selectedIds[i] = this.multipleSelection[i].id;
        }
        //console.log(this.selectedIds);
        deleteTestQuestion(this.selectedIds).then((resp) => {
          console.log(resp)
          if (resp.data.code === 1) {
            //删除成功
            this.$message.success("删除成功");
            this.clear();
          } else {
            this.$message.error(resp.data.msg);
            this.clear()
          }
        }).catch(() => {
        });

      }).catch(() => {
        //用户点击取消按钮
        this.$message.info("取消删除");
      });

    },

    //分页
    handleSizeChange(val) {
      // 重新设置每页显示的条数
      this.pageSize = val;
      this.show();
    },
    handleCurrentChange(val) {
      // 重新设置当前页码
      this.currentPage = val;
      this.show();
    },


    getMyInfo() {
      getInfo().then((res) => {
        if (res.data.code === 1) {
          this.myInfo = res.data.data;
          this.show();
        }
      }).catch((e) => {
      })
    },


  },
  mounted() {
    this.show();
    this.getMyInfo();
  },
};
</script>


<style>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}

* {
  margin: 0;
  padding: 0;
}

.el-header {
  background-color: rgb(238, 241, 246);
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: white;
}
</style>
<script setup>
</script>
